package com.quanquan.management.controller;

import DTO.PageDTO;
import base.BaseConst;
import com.quanquan.management.dto.ActivityApplyDTO;
import com.quanquan.management.dto.ActivityDTO;
import com.quanquan.management.dto.CouponDTO;
import com.quanquan.management.request.ActivityApplyRequestBody;
import com.quanquan.management.request.ActivityRequest;
import com.quanquan.management.request.SelfHelpIntegralRequest;
import com.quanquan.management.service.ActivityService;
import com.quanquan.management.service.CouponService;
import enums.ResCodeEnum;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import web.response.ResultBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 功能描述:活动后台管理
 *
 * @ClassName: ActivityComtroller
 * @Author: xiaoqianh丶
 * @Date: 2019-04-07 15:34
 * @Version: V1.0
 */
@Controller
@RequestMapping("/activity")
public class ActivityComtroller extends ManagerBaseController {

    private final ActivityService activityService;

    private final CouponService couponService;

    public ActivityComtroller(ActivityService activityService, CouponService couponService) {
        this.activityService = activityService;
        this.couponService = couponService;
    }

    @GetMapping("/list")
    public String list(@RequestParam(required = false) Integer pageSize,
                       @RequestParam(required = false) Integer page,
                       @RequestParam(required = false) String keyword,
                       @RequestParam(required = false) String sort,
                       @RequestParam(required = false) String fromDate,
                       @RequestParam(required = false) String activityStatus,
                       @RequestParam(required = false) String activityType,
                       @RequestParam(required = false) String toDate,
                       @RequestParam(required = false) boolean isAsc, Model model) {
        if (null == pageSize) {
            pageSize = 10;
        }
        if (null == page) {
            page = 0;
        } else if (page > 0) {
            page = page - 1;
        }
        ActivityRequest request = new ActivityRequest();
        request.setPage(page);
        request.setSize(pageSize);
        request.setKeyword(keyword);
        request.setFromDate(fromDate);
        request.setToDate(toDate);
        request.setActivityType(activityType);
        request.setActivityStatus(activityStatus);
        PageDTO<ActivityDTO> pageDTO = activityService.listActivitys(request);
        model.addAttribute("pageObj", pageDTO);
        return "activity/list";
    }

    /**
     * 添加
     *
     * @param requestBody
     * @param model
     * @return
     */
    @RequestMapping("/add")
    public String add(ActivityRequest requestBody, Model model) {
        List<CouponDTO> couponDTOS = couponService.couponList();
        model.addAttribute("couponDTOS", couponDTOS);
        if (BaseConst.RequestMethod.GET.equals(this.request.getMethod())) {
            return "activity/form";
        }
        boolean add = activityService.addActivity(requestBody);
        if (add) {
            return "redirect:/activity/list";
        } else {
            Map<String, Object> result = new HashMap<>();
            result.put("code", -1);
            result.put("msg", "添加失败");
            model.addAllAttributes(result);
            return null;
        }
    }

    /**
     * 编辑
     *
     * @param id
     * @param requestBody
     * @param model
     * @return
     */
    @RequestMapping("/edit")
    public String edit(String id, ActivityRequest requestBody, Model model) {
        List<CouponDTO> couponDTOS = couponService.couponList();
        model.addAttribute("couponDTOS", couponDTOS);
        if (BaseConst.RequestMethod.GET.equals(this.request.getMethod())) {
            ActivityDTO byId = activityService.findById(id);
            model.addAttribute("editable", byId);
            return "activity/form";
        }
        boolean update = activityService.update(requestBody);
        if (update) {
            return "redirect:/activity/list";
        } else {
            Map<String, Object> result = new HashMap<>();
            result.put("code", -1);
            result.put("msg", "修改失败");
            model.addAllAttributes(result);
            return null;
        }
    }

    @GetMapping("/delete")
    public String delete(@RequestParam String id, Model model) {
        boolean delete = activityService.delete(id);
        if (delete) {
            return "redirect:/activity/list";
        } else {
            model.addAttribute("code", -1);
        }
        return null;
    }

    @RequestMapping("/export")
    public ResultBody exportData(
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String fromDate,
            @RequestParam(required = false) String toDate,
            @RequestParam(required = false) Integer total,
            @RequestParam(required = false) String activityId

    ) {
        ActivityApplyRequestBody activityApplyRequestBody = new ActivityApplyRequestBody();
        activityApplyRequestBody.setKeyword(keyword);
        activityApplyRequestBody.setSize(total);
        activityApplyRequestBody.setFromDate(fromDate);
        activityApplyRequestBody.setToDate(toDate);
        activityApplyRequestBody.setActivityId(activityId);
        activityApplyRequestBody.setPage(0);
        activityApplyRequestBody.setSize(9999);

        try{
            activityService.export(this.response,activityApplyRequestBody);
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(),ResCodeEnum.SUCCESS.getMsg());
        }catch (Exception e){
            e.printStackTrace();
            return new ResultBody(ResCodeEnum.FAIL.getCode(),ResCodeEnum.FAIL.getMsg());
        }
    }


    @GetMapping("/list_record")
    public String listRecord(@RequestParam(required = false) Integer pageSize,
                             @RequestParam(required = false) Integer page,
                             @RequestParam(required = false) String keyword,
                             @RequestParam(required = false) String fromDate,
                             @RequestParam(required = false) String toDate,
                             @RequestParam(required = false) String id, Model model) {
        if (null == pageSize) {
            pageSize = 10;
        }
        if (null == page) {
            page = 0;
        } else if (page > 0) {
            page = page - 1;
        }
        ActivityApplyRequestBody request = new ActivityApplyRequestBody();
        request.setPage(page);
        request.setSize(pageSize);
        request.setKeyword(keyword);
        request.setFromDate(fromDate);
        request.setToDate(toDate);
        request.setActivityId(id);

        PageDTO<ActivityApplyDTO> activityApplyDTOPageDTO = activityService.listActivityApplyRecords(request);
        model.addAttribute("pageObj", activityApplyDTOPageDTO);
        model.addAttribute("activityId", id);

        return "activity/apply_record_list";
    }


}
